"""
 encoding    : utf-8
 @File       : common_function.py
 @Author     : qin
 @Version    :
 @Description:
 @Date       : 2024/07/10
"""
import os
import pymysql.cursors
import json


def getAbsPath(path):
    abspath = os.path.abspath(__file__)
    # print(abspath)
    cut_num = abspath.find('cloud_test')
    length = len('cloud_test')
    # print(cut_num)
    # print(abspath[:cut_num+length+1])
    return abspath[:cut_num + length + 1] + path


# print(getAbsPath(r'auto_code\data\add-staff-testcases.xlsx'))


class OperateMysql:
    '''查询返回数据格式为字典格式'''
    def __init__(self, dataname, ip='localhost', uname='root', pwd='123456', portnum=3306):
        self.conn = pymysql.connect(
            host=ip,
            port=portnum,
            user=uname,
            password=pwd,
            database=dataname,
            charset='utf8',
            cursorclass=pymysql.cursors.DictCursor
        )
        self.cursor = self.conn.cursor()

    def insertData(self, sql):
        pass

    def selectData(self, sql):
        self.cursor.execute(sql)
        result = self.cursor.fetchall()
        self.closeConn()
        return result

    def deleteData(self, sql):
        pass

    def updateData(self, sql):
        pass

    def closeConn(self):
        self.cursor.close()
        self.conn.close()

def getscreenshot(driver,file_name):
    abspath = getAbsPath(r'auto_code\log')
    path = abspath + '\\' + file_name + '.png'
    driver.get_screenshot_as_file(path)
def getData():
    from auto_code.common.common_function import getAbsPath
    import time
    import xlrd
    data_abspath = getAbsPath(r'auto_code\data\add-staff-testcases.xlsx')
    excel = xlrd.open_workbook(data_abspath)
    sheet1 = excel.sheet_by_index(0)
    row_num = sheet1.nrows
    sheet_data_list = []
    for i in range(1, row_num):
        if len(sheet1.row_values(i, 0, 5)[1]) == 0:
            break
        sheet_data_list.append(sheet1.row_values(i, 0, 5))
        pass
    # print(len(sheet_data_list))
    data_list = []
    for i in sheet_data_list:
        test_list = []
        test_list.append(i[0])
        test_list.append(i[1])
        test_list.append(json.loads(i[2]))
        test_list.append(json.loads(i[4]))
        test_list.append(i[3])
        data_list.append(test_list)
    return data_list

if __name__ == '__main__':
    # mysql = OperateMysql('cloud')
    # mark_content = '5555'
    # res = mysql.selectData(f'select username from user where username = {mark_content}')
    # print(res)
    print(type(getData()[0][2]))
